package com.wjc.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjc.wiki.entity.Ebook;
import com.wjc.wiki.entity.EbookExample;
import com.wjc.wiki.mapper.EbookMapper;
import com.wjc.wiki.req.EbookQueryReq;
import com.wjc.wiki.req.EbookSaveReq;
import com.wjc.wiki.resp.EbookQueryResp;
import com.wjc.wiki.resp.PageResp;
import com.wjc.wiki.util.CopyUtil;
import com.wjc.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EbookService {

    private static final Logger LOG= LoggerFactory.getLogger(EbookService.class);

    @Autowired
    private EbookMapper ebookMapper;

    @Resource
    private SnowFlake snowFlake;

    public PageResp<EbookQueryResp> list(EbookQueryReq req){

        EbookExample ebookExample =new EbookExample();
        EbookExample.Criteria criteria=ebookExample.createCriteria();

        if (!ObjectUtils.isEmpty(req.getName())){//动态sql,如果req的name不为空
            criteria.andNameLike("%"+req.getName()+"%");//使用where条件查询
        }

        if (!ObjectUtils.isEmpty(req.getCategoryId2())){//动态sql,如果req的categoryid2不为空
            criteria.andCategory2IdEqualTo(req.getCategoryId2());
        }
        //将查询到的对象转换为ebookresp,复制ebook对象到ebookresp
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Ebook> ebookList = ebookMapper.selectByExample(ebookExample);

        PageInfo<Ebook> pageInfo=new PageInfo<>(ebookList);
        LOG.info("总行数:{}",pageInfo.getTotal());
        LOG.info("总页数:{}",pageInfo.getPages());

//        List<EbookResp> respList =new ArrayList<>();
//        for (Ebook ebook : ebookList) {
////           EbookResp ebookResp = new EbookResp();
////           BeanUtils.copyProperties(ebook,ebookResp);//beanutil springboot自带,方便复制对象
//           //对象复制
//            EbookResp ebookResp = CopyUtil.copy(ebook,EbookResp.class);
//
//           respList.add(ebookResp);//将转换完成的对象添加到list中
//        }
        //列表复制
        List<EbookQueryResp> ebookRespList =CopyUtil.copyList(ebookList, EbookQueryResp.class);

        PageResp<EbookQueryResp> pageResq=new PageResp();
        pageResq.setTotal(pageInfo.getTotal());
        pageResq.setList(ebookRespList);

        return pageResq;
    }

    //保存
    public void save(EbookSaveReq req){
        Ebook ebook =CopyUtil.copy(req,Ebook.class);
        if (!ObjectUtils.isEmpty(req.getId())){//动态sql,如果req的id不为空,更新 否则新增
            //新增
            ebookMapper.updateByPrimaryKey(ebook);
        }else{
            ebook.setId(snowFlake.nextId());
            ebook.setDocCount(0);
            ebook.setViewCount(0);
            ebook.setVoteCount(0);
            ebookMapper.insert(ebook);
        }

    }

    public void delete(Long id){
        ebookMapper.deleteByPrimaryKey(id);
    }
}
